package org.mediahouse.trac.util;

import java.io.PrintWriter;

import org.mediahouse.trac.http.CustomHttpServlet.HttpRequestMethodType;

/**
 * @author Tin
 * @version %I%, %G%
 * 
 */
public final class SimpleTextHtmlWriter {
    private final PrintWriter out;

    public SimpleTextHtmlWriter(final PrintWriter out) {
        this(out, false);
    }

    public SimpleTextHtmlWriter(final PrintWriter out, final boolean autoFlush) {
        this.out = out;
        this.autoFlush = autoFlush;
        this.openForm = false;
    }

    public boolean checkError() {
        return this.out.checkError();
    }

    public SimpleTextHtmlWriter close() {
        this.out.close();
        return this;
    }

    private final boolean autoFlush;

    public SimpleTextHtmlWriter flush() {
        this.out.flush();
        return this;
    }

    private void autoFlush() {
        if (autoFlush) this.flush();
    }

    public boolean isAutoFlush() {
        return this.autoFlush;
    }

    private boolean openForm;

    public SimpleTextHtmlWriter openForm(final String action,
            final HttpRequestMethodType method) {
        this.out.write("<form action='" + action + "' method='" + method + "'>");
        this.openForm = true;
        return this;
    }

    public SimpleTextHtmlWriter closeForm() {
        if (openForm) this.out.write("</form>");
        openForm = false;
        return this;
    }

    public SimpleTextHtmlWriter h(final int level, final String html) {
        if (level < 1 || level > 6) throw new IllegalArgumentException("Heading Level must be between 1 and 6.");
        else this.out.write("<h" + level + ">" + html + "</h" + level + ">");
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h1(final String html) {
        this.h(1, html);
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h2(final String html) {
        this.h(2, html);
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h3(final String html) {
        this.h(3, html);
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h4(final String html) {
        this.h(4, html);
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h5(final String html) {
        this.h(5, html);
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter h6(final String html) {
        this.h(6, html);
        autoFlush();
        return this;
    }

    /**
     * @throws UnsupportedOperationException
     */
    public SimpleTextHtmlWriter html(final String html) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    public SimpleTextHtmlWriter input(final String name, final String type,
            final String value) {
        this.out.write("<input name='" + name + "' type='" + type + "' value='"
                + value + "' />");
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter textarea(final String name, final int cols,
            final int rows, final String html) {
        this.out.write("<textarea name='" + name + "' cols='" + cols
                + "' rows='" + rows + "'>" + html + "</textarea>");
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter submit(final String value) {
        this.out.write("<input type='submit' value='" + value + "' />");
        autoFlush();
        return this;
    }

    public SimpleTextHtmlWriter p(final String html) {
        this.out.write("<p>" + html + "</p>");
        autoFlush();
        return this;
    }

}